var host = "http://192.168.3.223:9000";
// var host = "https://dev.himeili.com";
const request = (url, optobj) => {
  return new Promise((resolve, reject) => {
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    });
    wx.request({
      url: host+url,
      method: optobj.method,
      data: optobj.data,
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      success(request) {
        wx.hideToast()
        if (request.data.code == 200) {
          resolve(request.data)
        } else {
          reject(request.data)
        }
      },
      fail(error) {
        reject(error.data)
      }
    })
  })
}

function requestAll(urlArr,optobj){
  return urlArr.map(function(e){
    return new Promise((resolve, reject) => {
      wx.showToast({
        title: '加载中',
        icon: 'loading'
      });
      wx.request({
        url: host+urlArr,
        method: optobj.method,
        data: optobj.data,
        header: {
          'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        success(request) {
          wx.hideToast()
          if (request.data.code == 200) {
            resolve(request.data)
          } else {
            reject(request.data)
          }
        },
        fail(error) {
          reject(error.data)
        }
      })
    })
  })
}

const get = (url, optobj = {}) => {
  return request(url, {
    method: 'GET',
    data: optobj
  })
}

const post = (url, optobj) => {
  return request(url, {
    method: 'POST',
    data: optobj
  })
}

const postAll = (urlArr, optobj) => {
  return requestAll(urlArr, {
    method: 'POST',
    data: optobj
  })
}



// const formatTime = date => {
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//   const hour = date.getHours()
//   const minute = date.getMinutes()
//   const second = date.getSeconds()

//   return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
// }

// const formatNumber = n => {
//   n = n.toString()
//   return n[1] ? n : '0' + n
// }

module.exports = {
  get,
  post,
  postAll
}